package com.jh.paymentchannel.payment.wechat;

import com.jh.paymentchannel.payment.PaymentRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author Yichuan
 * @since 1.0
 */
@Component
public class WechatConfig {

    public static final String SERVER_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    public static final String OAUTH2_URL = "https://api.weixin.qq.com/sns/oauth2/access_token";
    public static final String REDIRECT_URL = "https://open.weixin.qq.com/connect/oauth2/authorize";
    public static final String QUERY_URL = "https://api.mch.weixin.qq.com/pay/orderquery";

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;

    @Value("${wechat.apiSecret}")
    private String apiSecret;

    @Value("${wechat.mchId}")
    private String mchId;

    @Value("${wechat.mchKey}")
    private String mchKey;

    @Value("${wechat.notifyUrl}")
    private String notifyUrl;

    @Value("${wechat.redirectUri}")
    private String redirectUri;

    // 连接超时时间，单位毫秒
    private int httpConnectTimeoutMs = 6*1000;
    // 读数据超时时间，单位毫秒
    private int httpReadTimeoutMs = 8*1000;

    public String getAppId() {
        return appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getApiSecret() {
        return apiSecret;
    }

    public void setApiSecret(String apiSecret) {
        this.apiSecret = apiSecret;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getMchId() {
        return mchId;
    }

    public void setMchId(String mchId) {
        this.mchId = mchId;
    }

    public String getMchKey() {
        return mchKey;
    }

    public void setMchKey(String mchKey) {
        this.mchKey = mchKey;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public String getRedirectUri() {
        return redirectUri;
    }

    public void setRedirectUri(String redirectUri) {
        this.redirectUri = redirectUri;
    }

    public int getHttpConnectTimeoutMs() {
        return httpConnectTimeoutMs;
    }

    public void setHttpConnectTimeoutMs(int httpConnectTimeoutMs) {
        this.httpConnectTimeoutMs = httpConnectTimeoutMs;
    }

    public int getHttpReadTimeoutMs() {
        return httpReadTimeoutMs;
    }

    public void setHttpReadTimeoutMs(int httpReadTimeoutMs) {
        this.httpReadTimeoutMs = httpReadTimeoutMs;
    }

    /**
     * 获取微信code请求链接
     *
     * @return 返回code请求链接
     */
    public static String getRedirectUrl(String redirectUri, PaymentRequest paymentRequest) {
        return REDIRECT_URL + "?appid=wx161c977b22b97ece" +
                "&redirect_uri=" + redirectUri + paymentRequest.getParamUrl() +
                "&response_type=code" +
                "&scope=snsapi_base" +
                "&state=123#wechat_redirect";
    }

    /**
     * 获取openid请求的参数
     *
     * @return 返回请求参数
     */
    public String getOAuth2Param(String code) {
        return "appid=" + getAppId() + "&secret=" + getAppSecret() + "&code=" + code + "&grant_type=authorization_code";
    }
}
